﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC.Models;
using System.Web.Security;
using System.Data;


namespace MVC.Controllers
{
    public class EEController : Controller
    {

        private JardimEntities db = new JardimEntities();

        public void SetDb(JardimEntities novoDb)
        {
            db = novoDb;           
        }

        //
        // GET: /EE/
        [Authorize(Roles = "EE")]
        public ActionResult Index()
        {
           return View();
        }


        [Authorize(Roles = "EE")]
        public ActionResult EditarProfile()
        {
           
            Pessoa pessoa = (from p in db.Pessoas.ToList()
                    where p.UserId == Membership.GetUser(true).ProviderUserKey.ToString()                    
                    select p).FirstOrDefault(); 
            return View(pessoa);
        }


        [HttpPost]
        [Authorize(Roles = "EE")]
        public ActionResult EditarProfile(Pessoa p, int id)
        {
            p.PessoaId = id;
            
            if (ModelState.IsValid)
            {
                db.Entry(p).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }            
            return View(p);
        }


        [Authorize(Roles = "EE")]
        public ActionResult EscolherCrianca()
        {
            
             Crianca[] filhos = (from p in db.Criancas.ToList()
                                 from a in db.Pessoas.ToList()
                             where a.UserId == Membership.GetUser(true).ProviderUserKey.ToString()
                             && p.Pessoa == a
                              select p).ToArray();

             ViewData["Criancas"] = new SelectList(from items in filhos select items,"Nome", "Nome");
                      
            return View();
        }


        [Authorize(Roles = "EE")]
        [HttpPost]
        public ActionResult EscolherCrianca(Crianca crianca)
        {
            int id= (from p in db.Criancas.ToList()
                        where p.Nome == crianca.Nome
                         select p.CriancaId).FirstOrDefault();

            return RedirectToAction("InfoEducando", new { id= id });
        }

        [Authorize(Roles = "EE")]       
        public ActionResult InfoEducando(int id)
        {
            ViewBag.Crianca = db.Criancas.Find(id).Sala.SalaId;
            ViewBag.ID = id;
            return View();
        }

        [Authorize(Roles = "EE")]
        public ActionResult VerQuotas(int id)
        {
            ViewBag.ID = id;
            Quota[] quotas = (from p in db.Quotas.ToList()
                              where p.Crianca.CriancaId== id
                              select p).ToArray();
            return View(quotas);
        }

        [Authorize(Roles = "EE")]
        public ActionResult ListarLevaCriancas(int id)
        {
            ViewBag.ID = id;
            LevaCrianca[] lista = (from p in db.LevaCriancas.ToList()
                              where p.Crianca.CriancaId == id
                              select p).ToArray();
            return View(lista);
        }

        [Authorize(Roles = "EE")]
        public ActionResult CriarLevaCriancas(int id)
        {
            ViewBag.ID = id;            
           
            return View();
        }

        [Authorize(Roles = "EE")]
        [HttpPost]
        public ActionResult CriarLevaCriancas(LevaCrianca lc, int id)
        {
            LevaCrianca novaLevaCrianca = new LevaCrianca
            { 
                NomePessoa = lc.NomePessoa,
                BI = lc.BI,                           
                Crianca = (from p in db.Criancas.ToList()
                              where p.CriancaId == id                             
                              select p).FirstOrDefault()
            };
            
            if (ModelState.IsValid)
            {
                db.LevaCriancas.Add(novaLevaCrianca);
                db.SaveChanges();
                return RedirectToAction("ListarLevaCriancas", new { id = id });
            }
            ViewBag.ID = id;
            return View(lc);           
        }

        [Authorize(Roles = "EE")]
        public ActionResult ApagarLevaCriancas(int id, int idCrianca)
        {
            ViewBag.ID = idCrianca;
            LevaCrianca lc = db.LevaCriancas.Find(id);
            return View(lc);
        }

        [Authorize(Roles = "EE")]
        [HttpPost]
        public ActionResult ApagarLevaCriancas(int id,string idBack)
        {
            LevaCrianca lc = db.LevaCriancas.Find(id);
            db.LevaCriancas.Remove(lc);
            db.SaveChanges();
            int key = Int32.Parse(idBack);
            return RedirectToAction("ListarLevaCriancas", new { id = key });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        [Authorize(Roles = "EE")]
        public ActionResult DetalhesCrianca(int id)
        {

            Crianca crianca = db.Criancas.Find(id);

            return View(crianca);
        }

        [Authorize(Roles = "EE")]
        public ActionResult EditarCrianca(int id)
        {

           Crianca crianca = db.Criancas.Find(id);

            return View(crianca);
        }

        [HttpPost]
        [Authorize(Roles = "EE")]
        public ActionResult EditarCrianca(Crianca crianca, int id)
        {
         
            JardimEntities db2 = new JardimEntities();
            Crianca temp= db2.Criancas.Find(id);

            crianca.CriancaId = id;
            crianca.Pessoa = db2.Pessoas.Find(temp.Pessoa.PessoaId);
            crianca.Sala = db2.Salas.Find(temp.Sala.SalaId);
            db2.Dispose();
            if (ModelState.IsValid)
            {
                db.Entry(crianca).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(crianca);
        }

    }
}
